<template>
  <el-aside width="200px" class="sider-menu" :class="{ collapse: collapse }">
    <div class="sider-scroll">
      <div class="logo">
        <div class="tf-logo">
          <img :src="logo" alt />
        </div>
      </div>
      <el-menu router unique-opened :collapse="collapse" :default-active="activeIndex">
        <template v-for="n in menuData">
          <template v-if="n.show">
            <el-submenu v-if="n.children && n.children.length" :key="n.name" :index="n.path">
              <template slot="title">
                <i :class="n.icon"></i>
                <span slot="title">{{ n.name }}</span>
              </template>
              <el-menu-item v-for="item in n.children" :key="item.name" :index="item.path">
                <i v-if="item.icon" :class="item.icon"></i>
                {{ item.name }}
              </el-menu-item>
            </el-submenu>
            <el-menu-item v-else :key="n.name" :index="n.path">
              <i :class="n.icon"></i>
              <span slot="title">{{ n.name }}</span>
            </el-menu-item>
          </template>
        </template>
      </el-menu>
    </div>
  </el-aside>
</template>
<script>
import logo from '@/assets/imgs/logo.png';

export default {
  props: {
    collapse: {
      type: Boolean,
      default: false,
    },
    menuData: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      logo,
      activeIndex: '',
    };
  },
  computed: {},

  watch: {
    $route: function() {
      this.setActiveIndex();
    },
  },
  mounted() {
    this.setActiveIndex();
  },
  methods: {
    setActiveIndex() {
      this.$nextTick(() => {
        this.activeIndex = this.$route.path;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.tf-logo {
  transition: all 0.3s;
  overflow: hidden;
  display: inline-block;
}
.logo {
  height: 64px;
  position: relative;
  line-height: 64px;
  transition: all 0.3s;
  background: #002140;
  text-align: center;
  overflow: hidden;
  margin-bottom: 16px;
  a {
    display: block;
    overflow: hidden;
  }
  img {
    display: inline-block;
    vertical-align: middle;
    height: 40px;
  }
}
.sider-scroll {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: hidden;
  overflow-y: auto;
}
</style>

<style lang="less">
@white: rgba(255, 255, 255, 0.65);
.sider-menu {
  &.el-aside {
    box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    -webkit-box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    -moz-box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    -ms-box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    position: relative;
    z-index: 10;
    transition: all 0.3s;
    background-color: #001529;
    transition: all 0.3s;
    &.collapse {
      width: 64px !important;
      .tf-logo {
        width: 44px;
      }
    }
  }
  .el {
    &-menu {
      background: #001529;
      border-right: none;
      &-item {
        height: 40px;
        line-height: 40px;
        color: @white;
        transition: all 0.3s;
        &:focus,
        &:hover {
          background-color: transparent;
          color: white;
          i {
            color: white;
          }
        }
        &.is-active {
          background-color: #1890ff;
          color: white;
          i {
            color: white;
          }
        }
        i {
          transition: all 0.3s;
          color: @white;
          &.iconfont {
            margin-right: 6px;
            font-size: 18px;
          }
        }
      }
    }
    &-submenu {
      &.is-active {
        .el-submenu__title {
          color: white;
          background-color: #1890ff;
          i {
            color: white;
          }
        }
      }
      &.is-opened {
        .el-submenu__title {
          color: white;
          background-color: transparent;
          i {
            color: white;
          }
        }
      }
      &__title {
        height: 40px;
        line-height: 40px;
        color: @white;
        transition: all 0.3s;
        &:focus,
        &:hover {
          background-color: transparent;
          color: white;
          i {
            color: white;
          }
        }
        i {
          transition: all 0.3s;
          color: @white;
          &.iconfont {
            margin-right: 6px;
          }
        }
      }
      .el-menu {
        background-color: #000c17;
      }
    }
  }
}
.el {
  &-menu {
    &--popup {
      min-width: 160px;
      background-color: #001529;
      .el-menu {
        &-item {
          height: 40px;
          line-height: 40px;
          color: @white;
          transition: all 0.3s;
          &:focus,
          &:hover {
            background-color: transparent;
            color: white;
            i {
              color: white;
            }
          }
          &.is-active {
            background-color: #1890ff;
            color: white;
            i {
              color: white;
            }
          }
        }
      }
    }
  }
}
</style>
